class Input:
    __name = 'input'

    ''' 平凡图1 '''

    @staticmethod
    def get_graph1():
        v_set = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']
        s_set = {'ab': 4, 'bc': 6, 'cd': 10, 'da': 12, 'ef': 1, 'fg': 5, 'gh': 8, 'he': 11, 'ae': 2, 'bf': 3, 'cg': 7,
                 'dh': 9}
        return [v_set, s_set]

    ''' 平凡图2 '''

    @staticmethod
    def get_graph2():
        v_set = ['a', 'b', 'c', 'd', 'e', 'f']
        s_set = {'ab': 1, 'ac': 2, 'bc': 1, 'bd': 3, 'bf': 7, 'cd': 1, 'ce': 2, 'df': 3, 'ef': 6}
        return [v_set, s_set]

    ''' 二部图 '''

    @staticmethod
    def get_bipartite_graph():
        v_set1 = ['a', 'b', 'c', 'd', 'e']
        v_set2 = ['f', 'g', 'h', 'i', 'j']
        s_set = {'ag': 1, 'ah': 1, 'bf': 1, 'bg': 1, 'bi': 1, 'bj': 1, 'cg': 1, 'ch': 1, 'dg': 1, 'dh': 1, 'ei': 1,
                 'ej': 1}
        return [[v_set1, v_set2], s_set]

    ''' 完全二部图 '''

    @staticmethod
    def get_complete_bipartite_graph():
        v_set1 = ['a', 'b', 'c', 'd', 'e']
        v_set2 = ['f', 'g', 'h', 'i', 'j']
        s_set = {'af': 3, 'ag': 5, 'ah': 5, 'ai': 4, 'aj': 1,
                 'bf': 2, 'bg': 2, 'bh': 0, 'bi': 2, 'bj': 2,
                 'cf': 2, 'cg': 4, 'ch': 4, 'ci': 1, 'cj': 0,
                 'df': 0, 'dg': 1, 'dh': 1, 'di': 0, 'dj': 0,
                 'ef': 1, 'eg': 2, 'eh': 1, 'ei': 3, 'ej': 3}
        return [[v_set1, v_set2], s_set]

    ''' 输入图（集合） '''

    @staticmethod
    def input_graph(has_direction=False):
        v_set = []  # 顶点集
        s_set = {}  # 边集（元素为字典：key = s，value = weight）

        print('输入加入的顶点（每个顶点仅支持1个字符，如：a，输入end结束）:')
        while True:
            v_input = input('顶点')
            if v_input == 'end':
                break
            if len(v_input) != 1:
                print('点输入错误')
                continue
            if v_input in v_set:
                print('点输入错误')
                continue
            v_set.append(v_input)

        print('输入加入的边（每条边仅支持2个字符，如：ab，输入end结束）：')
        while True:
            s_input = input('边')
            if s_input == 'end':
                break
            if len(s_input) != 2:
                print('边输入错误')
                continue
            if s_input[0] not in v_set or s_input[1] not in v_set:
                print('边输入错误')
                continue
            if s_input in s_set:  # 边已经存在
                print('边输入错误')
                continue
            if not has_direction and s_input[::-1] in s_set:  # 无向图，边存在
                print('边输入错误')
                continue
            try:
                w_input = int(input('权重'))
            except ValueError:
                print('权重输入错误')
                continue
            if w_input == 'end':
                break
            if not isinstance(w_input, int):
                print('权重输入错误')
                continue
            s = {s_input: w_input}
            s_set.update(s)
        graph = [v_set, s_set]  # 图：点集，边集
        return graph

    ''' 输入图(邻接矩阵) '''

    @staticmethod
    def input_graph_matrix():
        v_set = []  # 顶点集
        s_set = []  # 边集（元素为字典：key = s，value = weight）

        print('输入加入的顶点（每个顶点仅支持1个字符，如：a，输入end结束）:')
        while True:
            v_input = input('顶点')
            if v_input == 'end':
                break
            if len(v_input) != 1:
                print('点输入错误')
                continue
            if v_input in v_set:
                print('点输入错误')
                continue
            v_set.append(v_input)

        print('输入加入边的邻接矩阵：')
        i = 0
        while True:
            if i == len(v_set):
                break
            line = []
            string = input()
            start = 0
            for j in range(len(string)):
                if string[j] == ' ':
                    end = j
                    try:
                        num = float(string[start:end])
                        num = float('Inf') if num == 0 else num
                        line.append(num)
                    except ValueError:
                        break
                    start = end + 1
                if j == len(string) - 1:
                    try:
                        num = float(string[start:])
                        num = float('Inf') if num == 0 else num
                        line.append(num)
                    except ValueError:
                        break

            if len(line) != len(v_set):
                print('输入有误')
                continue
            i = i + 1
            s_set.append(line)
        for i in range(len(v_set)):
            s_set[i][i] = float('Inf')

        graph = [v_set, s_set]  # 图：点集，边集
        return graph

    ''' 输入二部图（邻接矩阵） '''

    @staticmethod
    def input_bipartite_graph():
        v_set1 = []
        v_set2 = []
        s_set = []

        print('输入加入set1的顶点（每个顶点仅支持1个字符，如：a，输入end结束）:')
        while True:
            v_input = input('顶点')
            if v_input == 'end':
                break
            if len(v_input) != 1:
                print('点输入错误')
                continue
            if v_input in v_set1:
                print('点输入错误')
                continue
            v_set1.append(v_input)

        print('输入加入set2的顶点（每个顶点仅支持1个字符，如：a，输入end结束）:')
        while True:
            v_input = input('顶点')
            if v_input == 'end':
                break
            if len(v_input) != 1:
                print('点输入错误')
                continue
            if v_input in v_set1 or v_input in v_set2:
                print('点输入错误')
                continue
            v_set2.append(v_input)

        print('输入加入边的邻接矩阵：')
        i = 0
        while True:
            if i == len(v_set1):
                break
            line = []
            string = input()
            start = 0
            for j in range(len(string)):
                if string[j] == ' ':
                    end = j
                    try:
                        line.append(float(string[start:end]))
                    except ValueError:
                        break
                    start = end + 1
                if j == len(string) - 1:
                    try:
                        line.append(float(string[start:]))
                    except ValueError:
                        break

            if len(line) != len(v_set2):
                print('输入有误')
                continue
            i = i + 1
            s_set.append(line)

        graph = [[v_set1, v_set2], s_set]  # 图：点集，边集
        return graph
